var showingMenu = false;
var menuShowing = '';

function dropDownMenu(sender)
{
	// get menuName from the sender's id
	var menuName = sender.id.replace("_arrow", "");
	// make sure that all the other menus are not showing
	hideAllMenus(menuName);
	var menu = document.getElementById(menuName);
	if(isMenuShowing(menu))
		hideMenu(menu);
	else
		showMenu(menu, sender);
	showingMenu = true; /* so body onclick method doesn't override dropDownMenu*/
	return false;
}

function showMenu(menu, sender){
	menuShowing = menu.id;
	var liSelObj = sender.parentNode;

	// Change the styles to selected of both the arrow and the menuItem
	sender.className = "menu-arrow-selected";
	var itemA = getMenuItem(sender);
	itemA.className = "menu-item-selected";

	var targX = findPosX(liSelObj);
	
	//menu.style.width = width+'px';
	menu.style.left = targX+'px';
	menu.style.display = '';
}

function hideMenu(menu){
	menuShowing = '';
	var arrowA = document.getElementById(menu.id+'_arrow');
	arrowA.className = "menu-arrow";
	var itemA = getMenuItem(arrowA);
	itemA.className = "menu-item";

	menu.style.display = 'none';
}

function getMenuItem(menuArrow){
	var liSelObj = menuArrow.parentNode;
	return liSelObj.childNodes[0];
}

function isMenuShowing(menu){
	return (!(menu.style.display == 'none'));
}

function hideAllMenus(excludingName){
	if (showingMenu) {
		showingMenu = false;
		return;
	}
	if (menuShowing != '' && excludingName != menuShowing) {
		var menu = document.getElementById(menuShowing);
		hideMenu(menu);
	}
}


function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
    while(1)
    {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.x)
    curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
    while(1)
    {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.y)
    curtop += obj.y;
return curtop;
}

function doShow(elName, toggle, before, after) {
$(elName).toggle();
if($(elName).visible())
	$(toggle).update(after);
else $(toggle).update(before);
return false;
}

function doVideoShow(elName, toggle)
{
$(elName).toggle();
if($(elName).visible())
	$(toggle).innerHTML = $(toggle).innerHTML.replace("show_video", "hide_video");
else
	$(toggle).innerHTML = $(toggle).innerHTML.replace("hide_video", "show_video");
return false;
}

function doSwap(before, after) {
$(before).toggle();
$(after).toggle();
return false;
}

function doReport(userID,argumentID,spamType){
	var argTotal = $('yesno'+argumentID);
	argTotal.update('<span style="vertical-align:middle;"><img height="12px" width="12px" src="'+base_dir+'img/ajax-loader.gif" ></span>');

	var url = base_url+'spam/ajax_report/'+userID+'/'+argumentID+'/'+spamType;
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
			if(transport.responseText == "fail")
  			{
				alert("Please Try Again");
  			}
  			else
  			{
				return(doSwap('yesno'+argumentID,'confirm'+argumentID));
			}
		}
	});
	return false;
}

function doBan(debateKey,dispName,argumentID){
	var argTotal = $('banyesno'+argumentID);
	argTotal.update('<span style="vertical-align:middle;"><img height="12px" width="12px" src="'+base_dir+'img/ajax-loader.gif" ></span>');

	var url = base_url+'moderate/banUser/'+ debateKey +'/'+ dispName;
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
			if(transport.responseText == "fail")
  			{
				alert("Please Try Again");
  			}
  			else
  			{
				return(doSwap('banyesno'+argumentID,'banconfirm'+argumentID));
			}
		}
	});
	return false;
}

function doDismiss(eventType){

	var dismiss = $('dismiss'+eventType);
	dismiss.update('<span style="vertical-align:middle;"><img height="12px" width="12px" src="'+base_dir+'img/ajax-loader.gif" ></span>');

	var url = base_url+'browse/events/dismiss/'+ eventType;
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
			if(transport.responseText == "fail")
  			{
				alert("Please Try Again");
  			}
  			else
  			{
  				document.getElementById('loginEvents').update(transport.responseText);
			}
		}
	});

	return false;
}

function grabEvents(){

	var url = base_url+'browse/grabevents';
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) {
			if(transport.responseText == "fail")
  			{
				alert("Please Try Again");
  			}
  			else
  			{
  				document.getElementById('loginEvents').update(transport.responseText);
			}
		}
	});

	return false;
}

function useEffectBlinds(elName, toggle, before, after) {
element = $(elName);
if(element.style.display == 'none') {
  new Effect.BlindDown(element,{queue:'end',duration:0.3});
  $(toggle).update(after);
} else {
  new Effect.BlindUp(element,{queue:'end',duration:0.3});
  $(toggle).update(before);
}
return false;
}

function useEffectSlide(elName, toggle, before, after) {
element = $(elName);
if(element.style.display == 'none') {
  new Effect.SlideDown(element,{queue:'end',duration:0.3});
  $(toggle).update(after);
} else {
  new Effect.SlideUp(element,{queue:'end',duration:0.3});
  $(toggle).update(before);
}
return false;
}

function useEffectAppear(elName, toggle, before, after) {
element = $(elName);
if(element.style.display == 'none') {
  new Effect.Appear(element,{queue:'end',duration:0.3});
  $(toggle).update(after);
} else {
  new Effect.Fade(element,{queue:'end',duration:0.3});
  $(toggle).update(before);
}
return false;
}

function focusBtn(btnID, e)
{
	// if 'enter' key (ascii 13) change focus to btn and click
	// weird if for cross-brows, e = keyUp event
	if ((e.which?e.which:e.keyCode) == 13)
    {
		var a = document.getElementById(btnID); //alert(a);
		if(null != a){
			a.focus(); a.click();
			return false;
		}
	}
	return true;
}

function setSearchType(searchType)
{
	if (searchType ==  'debates') {
		$('search_users').className = 'searchLink';
		$('search_peak_users').hide();
		$('search_form').action = base_url+'browse/debates/all/search';
	}
	else
	{
		$('search_debates').className = 'searchLink';
		$('search_peak_debates').hide();
		$('search_form').action = base_url+'browse/users/all/search';
	}

	$('search_type').value = searchType;
	$('search_'+searchType).className = 'searchLinkSelected';
	$('search_peak_'+searchType).show();
}

function onOver_setSearchType(searchType)
{
	$('search_'+searchType).className = 'searchLinkSelected';
	$('search_peak_'+searchType).show();
}

function onOut_setSearchType(searchType)
{
	if ($('search_type').value != searchType) {
		$('search_'+searchType).className = 'searchLink';
		$('search_peak_'+searchType).hide();
	}
}

function submitForm(formName,ids,vals)
{
	for(i=0;i<ids.length;i++)
	{
		$(ids[i]).value = vals[i];
	}
	$(formName).submit();
	return false;
}
var submitStarted=false;
function oneSubmitOnly(form, btn) {
	if(!submitStarted) {
		$(btn).value = 'Saving';
		$(btn).disabled = true;
		submitStarted=true;
		form.submit();
	} else {}
	return false; // Return false stops submit if error
}

function trim(thisStr){
	return thisStr.replace(/^\s+|\s+$/g,"");
}

function expandVideo(divNums, divCount, toggle, before, after) {
var brokenstring=divNums.split(",");
var whichway=$(toggle).name;
var links=document.links.length;
divCount--;

for (var i = 0; i <= divCount; i++) {
	element = $(brokenstring[i]);
	if(whichway == "expand") {
		if(element.style.display == 'none') { 
			element.toggle();
		}
	} else {
		if(element.style.display != 'none') { 
			element.toggle();
		}
	}
}

if(whichway == "expand") {
	$(toggle).update(after);
	$(toggle).name = "contract";
	for (var i = 0; i <= links; i++) {
		lnk = $(document.anchors[i]);
		if(lnk.name == "showmore") {
			lnk.update("&#8593; Hide Video");
		}
	}
} else {
	$(toggle).update(before);
	$(toggle).name = "expand";
	for (var i = 0; i <= links; i++) {
		lnk = $(document.anchors[i]);
		if(lnk.name == "showmore") {
			lnk.update("&#8595; Show Video");
		}
	}
}
return false;
}